package com.ruipengkj.view.reserveView;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.commons.util.SwingUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.view.base.BaseInternalFrame;
/**
 * 库存管理主界面
 *
 */
public class ReserveInternalFrame extends BaseInternalFrame {

	private static final long serialVersionUID = 1L;

	private JToolBar toolBar;//工具栏
	
	private JPanel main;//主面板
	private CardLayout main_layout;//主面板布局
	
	private JButton commodityDamageButton;//商品报损按钮
	private CommodityDamagePanel commodityDamagePanel;//商品报损面板
	
	private JButton articlesUsingButton;//物品领用按钮
	private ArticlesUsingPanel articlesUsingPanel;//物品领用面板
	
	private JButton otherReserveButton;//其他方式出库存单
	private OtherReservePanel otherReservePanel;//其他方式出库存单面板
	
	private JButton ReserveAllotButton;//仓库调拨按钮
	private StoreTransferPanel reserveAllotPanel;//仓库调拨面板
	
	private JButton stockAlarmButton;//库存报警按钮
	private StockAlarmPanel stockAlarmPanel;//库存报警面板
	
	private JButton stockInOutQueryButton;//出入库存单查询按钮
	private StockInOutQuery stockInOutQuery;//出入库存单查询面板
	
	public ReserveInternalFrame(String title, boolean resizable,
			boolean closable, boolean maximizable, boolean iconifiable) {
		super(title, resizable, closable, maximizable, iconifiable);
		init();
	}


	private void init(){
		this.setLayout(new BorderLayout());
		this.initComponents();
		this.initListener();
	}
	
	//初始化组件
	private void initComponents() {
		toolBar = new JToolBar();
		toolBar.setFloatable(false);//设置工具栏不可移动
		ReserveAllotButton=new JButton("仓库调拨",SwingUtil.getOpenSwingImage("reserve_store_allocation.png"));
		commodityDamageButton=new JButton("商品报损",SwingUtil.getOpenSwingImage("reserve_commodity_reported.png"));
		articlesUsingButton=new JButton("物品领用",SwingUtil.getOpenSwingImage("reserve_items_requisitioned.png"));
		otherReserveButton=new JButton("其他方式出库存单",SwingUtil.getOpenSwingImage("reserve_other_out_of_stock_single.png"));
		stockAlarmButton=new JButton("库存报警",SwingUtil.getOpenSwingImage("reserve_stock_alarm.png"));
		stockInOutQueryButton=new JButton("出入库存单查询",SwingUtil.getOpenSwingImage("reserve_access_to_inventory_list_query.png"));
		
		if(ApplicationContext.TRIGHT_LIST.contains(ApplicationContext.RIGHT_LIVEL61)){
			toolBar.add(ReserveAllotButton);
		}
		if(ApplicationContext.TRIGHT_LIST.contains(ApplicationContext.RIGHT_LIVEL62)){
			toolBar.add(commodityDamageButton);
		}
		if(ApplicationContext.TRIGHT_LIST.contains(ApplicationContext.RIGHT_LIVEL63)){
			toolBar.add(articlesUsingButton);
		}
		if(ApplicationContext.TRIGHT_LIST.contains(ApplicationContext.RIGHT_LIVEL64)){
			toolBar.add(otherReserveButton);
		}
		if(ApplicationContext.TRIGHT_LIST.contains(ApplicationContext.RIGHT_LIVEL65)){
			toolBar.add(stockAlarmButton);
		}
		if(ApplicationContext.TRIGHT_LIST.contains(ApplicationContext.RIGHT_LIVEL66)){
			toolBar.add(stockInOutQueryButton);
		}
		
		main=new JPanel();
		main_layout=new CardLayout();
		main.setLayout(main_layout);
		
		this.add(toolBar,BorderLayout.NORTH);
		this.add(main,BorderLayout.CENTER);
	}
	//初始化监听器
	private void initListener() {
		ReserveAllotButton.addActionListener(this);
		commodityDamageButton.addActionListener(this);
		articlesUsingButton.addActionListener(this);
		otherReserveButton.addActionListener(this);
		stockAlarmButton.addActionListener(this);
		stockInOutQueryButton.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==ReserveAllotButton){
			//仓库调拨按钮
			reserveAllotPanel = new StoreTransferPanel();
			main.add(reserveAllotPanel,"reserveAllotPanel");
			main_layout.show(main, "reserveAllotPanel");
			this.setTitle("库存管理--仓库调拨");
			Util.setButtonStatus(toolBar,ReserveAllotButton);
			SwingUtilities.invokeLater(reserveAllotPanel);
		} else if(e.getSource()==commodityDamageButton){
			//商品报损按钮
			commodityDamagePanel = new CommodityDamagePanel();
			main.add(commodityDamagePanel,"commodityDamagePanel");
			main_layout.show(main, "commodityDamagePanel");
			this.setTitle("库存管理--商品报损");
			Util.setButtonStatus(toolBar,commodityDamageButton);
			SwingUtilities.invokeLater(commodityDamagePanel);
		} else if(e.getSource()==articlesUsingButton){
			//物品领用按钮
			articlesUsingPanel = new ArticlesUsingPanel();
			main.add(articlesUsingPanel,"articlesUsingPanel");
			main_layout.show(main, "articlesUsingPanel");
			this.setTitle("库存管理--物品领用");
			Util.setButtonStatus(toolBar,articlesUsingButton);
			SwingUtilities.invokeLater(articlesUsingPanel);
		} else if(e.getSource()==otherReserveButton){
			//其他方式出库存单按钮
			otherReservePanel = new OtherReservePanel();
			main.add(otherReservePanel,"otherReservePanel");
			main_layout.show(main, "otherReservePanel");
			this.setTitle("库存管理--其他方式出库存单");
			Util.setButtonStatus(toolBar,otherReserveButton);
			SwingUtilities.invokeLater(otherReservePanel);
		} else if(e.getSource()==stockAlarmButton){
			//库存报警按钮
			stockAlarmPanel = new StockAlarmPanel();
			main.add(stockAlarmPanel,"stockAlarmPanel");
			main_layout.show(main, "stockAlarmPanel");
			this.setTitle("库存管理--库存报警");
			Util.setButtonStatus(toolBar,stockAlarmButton);
			SwingUtilities.invokeLater(stockAlarmPanel);
		} else if(e.getSource()==stockInOutQueryButton){
			//出入库存单查询按钮
			stockInOutQuery = new StockInOutQuery();
			main.add(stockInOutQuery,"stockInOutQuery");
			main_layout.show(main, "stockInOutQuery");
			this.setTitle("库存管理--出入库存单查询");
			Util.setButtonStatus(toolBar,stockInOutQueryButton);
			SwingUtilities.invokeLater(stockInOutQuery);
		}
		main.updateUI();
	}
	
	public void showButton(){
		if (toolBar != null && toolBar.getComponentCount() != 0)
			((JButton)toolBar.getComponent(0)).doClick();

	}
	
	public void showButton(int type){
		switch(type){
		case 1:{ReserveAllotButton.doClick();break;}
		case 2:{commodityDamageButton.doClick();break;}
		case 3:{articlesUsingButton.doClick();break;}
		case 4:{otherReserveButton.doClick();break;}
		case 5:{stockAlarmButton.doClick();break;}
		case 6:{stockInOutQueryButton.doClick();break;}
	}
	}
}